package com.siam.package_rider.mapper;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Param;

import com.siam.package_rider.entity.TbDeliveryOrder;
import com.siam.package_rider.model.param.TbDeliveryOrderParam;
import org.apache.ibatis.annotations.Select;

/**
 * ---------------------------
 * 配送订单表 (TbDeliveryOrderMapper)         
 * ---------------------------
 * 作者：  高品
 * 时间：  2025-03-29 18:55:18
 * ---------------------------
 */
public interface TbDeliveryOrderMapper extends BaseMapper<TbDeliveryOrder> {

    /**
     * 基础分页查询
     * @param param
     * @return
     */    
    Page<TbDeliveryOrder> page(@Param("page") Page page, @Param("param") TbDeliveryOrderParam param);

    @Select("<script>SELECT DATE_FORMAT(create_time, '%Y-%m-%d') AS orderDate, COUNT(*) AS orderCount, IFNULL(SUM(actual_price), 0) AS orderAmount FROM tb_delivery_order " +
            "<where> STATUS in (5) " +
            "<if test=\"shopId != null\"> AND shop_id = #{shopId} </if>" +
            "</where> GROUP BY DATE_FORMAT(create_time, '%Y-%m-%d')" +
            "</script>")
    List<Map<String, Object>> selectStatisticOrder(@Param("shopId") Integer shopId);

    @Select("<script>SELECT DATE_FORMAT(create_time, '%Y-%m-%d') AS orderDate FROM tb_delivery_order " +
            "<where> STATUS = 5 " +
            "<if test=\"shopId != null\"> AND shop_id = #{shopId} </if>" +
            "</where> order by create_time asc limit 1 " +
            "</script>")
    String selectStartDateOrder(@Param("shopId") Integer shopId);

    @Select("<script>select IFNULL(sum(o.actual_price), 0) from tb_delivery_order as o "+
            "<where> 1=1 and STATUS in (5) and (o.create_time between #{order.startTime} and #{order.endTime})" +
            "<if test=\"order.shopId != null\"> AND o.shop_id = #{order.shopId} </if>" +
            "</where>"+
            "</script>")
    BigDecimal selectSumActualPrice(@Param("order") TbDeliveryOrderParam param);

    @Select("<script>select count(*) from tb_delivery_order as o "+
            "<where> 1=1 and STATUS in (5) and (o.create_time between #{order.startTime} and #{order.endTime})" +
            "<if test=\"order.riderId != null\"> AND o.rider_id = #{order.riderId} </if>" +
            "<if test=\"order.shopId != null\"> AND o.shop_id = #{order.shopId} </if>" +
            "</where>"+
            "</script>")
    Integer selectCountCompleted(@Param("order") TbDeliveryOrderParam param);
}